import Vue from 'vue';
import axios from 'axios'
import qs from 'qs'
import { Toast } from 'vant';

//超时时间
axios.defaults.timeout = 5000;
//默认头部类型
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';

axios.interceptors.request.use((config) => {
    Toast.loading({mask: true, message: '加载中...'});
	if(config.method  === 'post'){
	    config.data = qs.parse(config.data);
	}
  	return config;
},(error) =>{
    Toast.fail('请求发生异常,请重试');return false;
});

axios.interceptors.response.use((res) =>{
    Toast.clear();
    var data = res.data;
    if(data['code']==-1000){
        window.location.href="/auth/#/?path="+encodeURI(data['path']);return false;
    }
    if(data['code']==10){
        Toast.fail(data['msg']);
        return false;
    }else if(data['code']==0 && data['msg']!=''){
        Toast.success(data['msg']);
    }
    return res;
}, (error) => {
    Toast.fail('获取数据发生异常,请重试');return false;
});

export default axios